package com.cx.gateway.menu;

import com.cx.gateway.redis.RedisConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 */
@Service
public class MenuService {

    private static final Logger log = LoggerFactory.getLogger(MenuService.class);
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    private ConcurrentHashMap<String, List<String>> menuMap = new ConcurrentHashMap<String, List<String>>();

    @PostConstruct
    @Scheduled(cron = "0 30/* 0 0 0")
    public synchronized void updateMenuInfo() {
        log.info("加载权限信息到内存中");
        //加载redis中权限到内存
        Map<Object, Object> object = redisTemplate.opsForHash().entries(RedisConstant.MENU_INFO.getKey());
        object.forEach((k, v) -> {
            menuMap.put(k.toString(), (ArrayList<String>) v);
        });
        log.info("加载完成");
    }

    public List<String> getMenuList(String key) {
        return menuMap.get(key);
    }
}
